package com.fzy.common.encode;

import java.io.UnsupportedEncodingException;

public class Native2AsciiUtil {
    private Native2AsciiUtil() {
    }

    /**
     * 可以将GBK转换成为UTF-8编码
     *
     * @param str
     * @return
     */
    public static String native2ascii(String str) {
        if (str != null) {
            char[] ac = str.toCharArray();
            StringBuffer rs = new StringBuffer(ac.length);
            for (int k = 0; k < ac.length; k++)
                if (ac[k] > '\177') {
                    rs.append((char) 92);
                    rs.append((char) 117);
                    String s1 = Integer.toHexString(ac[k]);
                    StringBuilder stringbuffer = new StringBuilder(s1);
                    stringbuffer.reverse();
                    int l = 4 - stringbuffer.length();
                    for (int i1 = 0; i1 < l; i1++) {
                        stringbuffer.append('0');
                    }
                    for (int j1 = 0; j1 < 4; j1++) {
                        rs.append(stringbuffer.charAt(3 - j1));
                    }

                } else {
                    rs.append(ac[k]);
                }
            return rs.toString();
        }
        return null;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(Native2AsciiUtil.native2ascii("我们是中国人"));
    }
} 
 